<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="560px"
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="fleetModel" :rules="formfleet"
      ref="FleetForm" label-width="80px" label-position="right" size="small">
      <el-form-item label="车队名称" prop="name" label-width="120px">
        <el-col :span="16"><el-input :maxlength="32" v-model="fleetModel.name" placeholder="请输入车队名称"></el-input></el-col>
      </el-form-item>
       <el-form-item label="负责人" prop="manager" label-width="120px">
        <el-col :span="16">
         <el-select v-model="fleetModel.manager" :maxlength="32"  placeholder="请输入负责人">
           <el-option></el-option>
         </el-select>
        </el-col>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('FleetForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import teamApi from '@/api/team.js'
export default {
   name: "fleet-edit",
    data() {
    return {
      loading: false,
      fleetModel: {
        name:"",
        manager: ''
      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      formfleet: {
          name:[{
              required: true, message: '请输入车队名称', trigger: 'blur'
          }],
          manager:[{
              required:true,message:"请输入负责人",trigger:'blur'
          }]
      }
    }
 },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveFleet()
          } else {
            this.doUpdateFleet()
          }
        } else {
          return false
        }
      })
    },
    doUpdateFleet() {
      // this.loading = true
      // updateEmp(this.EmpModel)
      // .then(response => {
      //   this.loading = false
      //   this.dialogProps.visible = false
      //   this.$emit('save-finished')
      // })
      // .catch(error => {
      //   this.outputError(error)
      // })
    },
    doSaveFleet() {
      this.loading = true
      teamApi.saveFleet(this.fleetModel)
      .then(res => {
        console.log(res)
       this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(err => {
        console.error(err);
      })
    },
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['FleetForm'].clearValidate()
      })
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })
    }
  },
   mounted: function() {
    this.$nextTick(() => {
      this.$on('openEditFleetDialog', function() {
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '编辑车队'
        this.dialogProps.visible = true
      })
      this.$on('openAddFleetDialog', function() {
        this.dialogProps.action = 'add'
        this.dialogProps.title = '新增车队'
        this.dialogProps.visible = true
      })
    })
  }
}
</script>

<style>

</style>
